home *** CD-ROM | disk | FTP | other *** search
/ infoROM 17,000 Product Descriptions for Business / infoROM Product Descriptions for Business - ESX Interactive.ISO / argdemos / nexsys / structur.atl < prev    next >
Encoding:
Text File  |  1993-06-21  |  4.7 KB  |  153 lines

  1. \
  2. \  STRUCTURE.ATL
  3. \
  4. \  Copyright (C) 1993 by Derrick Oswald
  5. \
  6. \  Derrick Oswald
  7. \  Nexsys Consulting Inc.
  8. \  44 Douglas Drive
  9. \  Ayr, Ontario
  10. \  N0B 1E0
  11. \  (519) 632-8243
  12. \  (519) 632-8244 FAX
  13.  
  14. \  Description:
  15. \       Allows aggregates of data to be described as structures. General-
  16. \       ization of structures in traditional programming languages. Allows
  17. \       definition, initialization and action part. Basic object based
  18. \       action may be defined in a style similar to the "does" section of
  19. \       a creating word.
  20. \
  21.  
  22. .( "\nLoading Structures definitions..."
  23.  
  24. \ field - creates a field definition at compile time
  25. \         at run time adds the field offset to the structure address
  26. : field ( offset -> )
  27.     constant
  28.   does>
  29.     @ + ;
  30.  
  31. \ size of compiled item
  32. 4 constant cell
  33.  
  34. 0 field ->StructureSize ( struct.type -- addr)
  35. cell field ->Initiate ( struct.type -- addr)
  36.  
  37. : as ( -- struct.type)  
  38.   ' >body                               ( Quote next symbol and access body)
  39.   \ this should be
  40.   \   [compile] literal                 ( If compiling generate a literal)
  41.   \ but atlas complains if compiler words are used outside a : definition
  42.   state @ if
  43.     compile (lit) ,
  44.   then
  45. ; immediate
  46.  
  47. : this ( -- addr)
  48.   last >body                            ( Access the body of the last symbol)
  49. ;
  50.  
  51. : initiate ( addr struct.type -- )  
  52.   ->Initiate @ ?dup                     ( Access initiate. code pointer)
  53.   if >r else drop then                  ( If available perform initialization)
  54. ;
  55.  
  56. \ make-struct - reserve memory for a structure and initialize
  57. : make-struct ( struct.type -- addr)
  58.   here dup >r                           ( Save pointer to instance)
  59.   over ->StructureSize @ allot          ( Access size and allocate memory)
  60.   swap initiate r>                      ( Perform initialization)
  61.  
  62. \ ?compile - compile or execute next threaded word depending on state
  63. : ?compile ( -- )
  64.   state @ if
  65.     r> dup , 4 + >r
  66.   then
  67. ;
  68.  
  69. \ new-struct - create structure of following type
  70. : new-struct ( -- addr)
  71.   [compile] as                          ( Take the next symbol, "as")
  72.   ?compile make-struct                  ( And "make" an instance)
  73. ; immediate
  74.  
  75. \ sizeof - return the size of a structure
  76. : sizeof ( -- num)
  77.   ' >body ->StructureSize @             ( Access size of structure)
  78.   \ this should be
  79.   \   [compile] literal                 ( And make literal if compiling)
  80.   \ but atlas complains if compiler words are used outside a : definition
  81.   state @ if
  82.     compile (lit) ,
  83.   then
  84. ; immediate
  85.  
  86. \ assign - set structure data
  87. : assign ( a b -- )  
  88.   [compile] sizeof ?compile cmove       ( Access size and assign instance)
  89. ; immediate
  90.  
  91. \ .( "\nnot-equal"
  92. \ : not-equal ( a b -- bool)
  93. \  [compile] sizeof ?compile -match     ( Access size and match the blocks)
  94. \ ; immediate
  95.  
  96. \ struct.type - lead in word for structure definition
  97. : struct.type ( -- struct.type offset0)  
  98.   create
  99.   here 4 + \ add 4 because ATLAS moves the word when does> encountered
  100.   0 0 , 0 ,                             ( Allocate initial struct information)
  101. does> ( struct.type -- )
  102.   create make-struct drop               ( Create a new instance)
  103. ;
  104.  
  105. : bytes ( offset1 n -- offset2)
  106.   over field +
  107. \  over dup                              ( Check for zero offset)
  108. \  if field +                            ( Create an access field of "n" bytes)
  109. \  else
  110. \    create , + immediate                ( Create an efficient field)
  111. \    does> ( field -- )
  112. \      drop                              ( Does nothing at runtime )
  113. \  then
  114. ;
  115.  
  116. : align ( offset1 -- offset2)  
  117.   dup 1 and +                           ( Align field offset to even address)
  118. ;
  119.  
  120. : struct.field ( bytes -- )  
  121.   create , 0 ,                          ( Create a predefined field type)
  122. does> ( struct.field -- )
  123.   @ bytes                               ( At run-time create field names)
  124.  
  125. : struct ( -- )  
  126.   [compile] sizeof bytes                ( Create a structure sized field name)
  127. ;
  128.  
  129. ( Initial set of field names)
  130. 1 struct.field byte ( -- )
  131. 2 struct.field word ( -- )
  132. 4 struct.field long ( -- )
  133. 4 struct.field ptr  ( -- )
  134. 4 struct.field enum ( -- )
  135.  
  136. : struct.init ( struct.type offset3 -- )
  137.   align over ->StructureSize !          ( Assign size of structure type)
  138.   here swap ->Initiate ]                ( And pointer to initialization code)
  139. ;
  140.  
  141. : struct.does ( -- ) 
  142.   [compile] does>                       ( Do what does-does)
  143. ; immediate
  144.  
  145. : struct.end ( [] or [struct.type offset3] -- )  
  146.   state @                               ( Check compilation status)
  147.   if ['] EXIT , [COMPILE] [             ( If compiling then end definition)
  148.   else swap ->StructureSize ! then      ( Else assign size of structure type)
  149. ; immediate
  150.  
  151. .( "\nLoaded.\n"